home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15580 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  68 lines

  1. Path: news.cityu.edu.hk!92907308
  2. From: 92907308@cpccux0.cityu.edu.hk ()
  3. Newsgroups: comp.lang.c++
  4. Subject: 2D pointer array to class overwritten
  5. Date: 7 Apr 1996 11:35:53 GMT
  6. Organization: City University of Hong Kong
  7. Message-ID: <4k89ap$7fl@ctylnk.cityu.edu.hk>
  8. NNTP-Posting-Host: cpccux1.cityu.edu.hk
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11.  
  12. Hello,
  13.  
  14. I have a difficulty with a 2D array of pointers to a class.
  15. The addresses of new objects overwrite the addresses of the objects
  16. at the upper region of the array. The program is :
  17.  
  18. #include <fstream.h>
  19. class cl
  20. {
  21.     int a;
  22. };
  23.  
  24. void main(void)
  25. {
  26.     cl *ptr[4,4];
  27.     int i,j;
  28.  
  29.     for (i=1; i<=3; i++)
  30.         {
  31.             cout << "\n";
  32.             for (j=1; j<=3; j++)
  33.                 {
  34.                 ptr[i,j]=new cl;
  35.                 cout << "  "<< ptr[i,j];
  36.                 }
  37.         }
  38.     cout << "\n";
  39.     for (i=1; i<=3; i++)
  40.         {
  41.         cout << "\n";
  42.         for (j=1; j<=3; j++)
  43.             cout << "  "<< ptr[i,j];
  44.         }
  45. }
  46.  
  47.  
  48. And it produces the following output:
  49.  
  50.   0x23530ef6  0x23530efe  0x23530f06
  51.   0x23530f0e  0x23530f16  0x23530f1e
  52.   0x23530f26  0x23530f2e  0x23530f36
  53.  
  54.   0x23530f26  0x23530f2e  0x23530f36
  55.   0x23530f26  0x23530f2e  0x23530f36
  56.   0x23530f26  0x23530f2e  0x23530f36
  57.  
  58.  
  59. The address assignment statement seems to assign the newly constructed
  60. objects to the whole [x,1] region. Why?
  61.  
  62. I surely want the array of address of the first part
  63. of the output. How can I fix this?
  64.  
  65. Thanks a lot and please reply by mail.
  66.  
  67. Ken
  68.